package command;

import java.util.List;

import model.impl.ScheduleImpl;
import DTO.PassengerDTO;
import DTO.ScheduleDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;

/**
 * Class represents command interface;
 * It sends all the passenger on the schedule object to the administer user. 
 * To send them it uses DTO. If there is any passengers it sends message OK 
 * and the list of passengers, else sends no and null.
 */
public class AdminViewPassengersOnTrain implements Command {

	private Protocol protocol = new Protocol();
	
	@Override
	public void execute(ReceivedData recived) {
		model.Schedule scheduleImpl = new ScheduleImpl();
		Protocol.Message message = this.protocol.decodeMessage(recived.data);
		ScheduleDTO scheduleDTO = (ScheduleDTO) message.getObject();
		List<PassengerDTO> passengers = scheduleImpl.getAllPassengersOnSchedule(scheduleDTO);
		if (passengers != null){
			Protocol.Message answer = this.protocol.new Message("ok", passengers);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		} else {
			Protocol.Message answer = this.protocol.new Message("no", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		}		
	}
}
